package com.lanqiao.attendance.controller;

import com.alibaba.fastjson.JSON;
import com.lanqiao.attendance.pojo.Student;
import com.lanqiao.attendance.pojo.User;
import com.lanqiao.attendance.service.StudentService;
import com.lanqiao.attendance.utils.JWTUtils;
import com.lanqiao.attendance.vo.LoginVo;
import com.lanqiao.attendance.vo.PageParam;
import com.lanqiao.attendance.vo.Result;
import com.lanqiao.attendance.vo.StudentListVo;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.security.interfaces.RSAKey;
import java.util.List;

@RestController
@RequestMapping("student")
public class StudentController {

    @Autowired
    StudentService studentService;

    @Autowired
    FileController fileController;



    @GetMapping("queryById")
    public Result queryAll(Long  requestParam){
        System.out.println(requestParam);
        return Result.success("ok",requestParam);
    }

    /**
     * 查询学生所有信息
     *
     * @return
     */
    @PostMapping("query/all")
    public Result queryAllStu(@RequestBody PageParam pageParam) {

        return Result.success("查询成功", studentService.queryAllStu(pageParam));
    }

    /**
     * 使用账号密码登录
     * @param account
     * @param password
     * @return
     */

    /**
     * 插入学生信息，只插入部分信息
     *
     * @param student
     * @return
     */
    @PostMapping("insert")
    public Result insertStudent(@RequestBody Student student) {
        System.out.println(student);
        if (studentService.queryStuByAccount(student.getAccount()) != null) {
            return Result.fail(-999, "插入失败,已存在该账号");
        }
        int i = studentService.insertStudent(student);
        student = studentService.queryStuByAccount(student.getAccount());
        return Result.success("插入成功", student);
    }

    /**
     * 根据名字查询学生部分信息
     *
     * @param
     * @return
     */
    @PostMapping("query/StuByName")
    public Result queryStuByName(@RequestBody Student student1) {
        if (student1.getName() == null || student1.getName() == "") {
            return Result.success("okk", studentService.queryAllStu(new PageParam(1, 10)));
        }
        List<Student> student = studentService.queryStuByName(student1.getName());
        if (student.size() == 0) {
            return Result.fail(-999, "没有该学生信息");
        }
        return Result.success("查询成功", student);
    }

    /**
     * 单独上传文件的接口
     * 上传文件的请求必须是post
     *
     * @param file
     * @return
     */
    @RequestMapping(value = "upload", method = RequestMethod.POST)
    public Result upload(MultipartFile file) {
       return fileController.submitImg(file,null);
        //保存到什么地方
//        if (file != null) {
//            //进行重命名
//            String fileEndName = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
//            String newFileName = String.valueOf(System.currentTimeMillis()) + fileEndName;
//            if (!(fileEndName.equals(".jpg") || fileEndName.equals(".png") || fileEndName.equals(".jpeg"))) {
//                return Result.fail(500, "上传文件格式不符合");
//            }
//            try {
//                file.transferTo(new File(uploadFile + "\\" + newFileName));
//            } catch (Exception e) {
//                return Result.fail(500, "上传文件失败:[" + e.getMessage() + "]");
//            }
//            return Result.success("上传文件成功[" + file.getOriginalFilename() + "]成功", newFileName);
//        }
//        return Result.fail(501, "上传文件为空");
    }

    @PutMapping("update/StuClass")
    public Result updateStuClass(@RequestParam("className") String className,
                                 @RequestParam("id") long id) {
        int i = studentService.updateStuClass(className, id);
        if (studentService.queryClass(className) == null) {
            return Result.fail(-999, "没有该班级，请重新分配");
        }
        Student student = studentService.queryStuById(id);
        return Result.success("班级分配成功", student);
    }

    @PostMapping("deleteByAccount")
    public Result deleteStuById(@RequestBody Student student1) {
        String account = student1.getAccount();
        System.out.println(account);
        Student student = studentService.queryStuByAccount(account);
        if (student == null) {
            return Result.fail(-999, "删除失败，没有该学生信息");
        }
        int i = studentService.delStuById(account);
        return Result.success("删除成功", student);
    }

    //    通过账号学生更新自己的全部信息
    @PostMapping("update/StuByAccount")
    public Result updateStuByStu(@RequestBody Student student) {
        int i = studentService.updateStuByAccount(student);
        Student student1 = studentService.queryStuById(student.getId());
        if (student1 != null) {
            return Result.success("更新成功", student1);
        } else {
            return Result.fail(400, "出错了");
        }
    }

    //查询所有为分配班级的学生
    @PostMapping("queryAllNoClass")
    public Result queryAllNoClass(){
        List<Student> studentList = studentService.queryAllNoClass();
        if(studentList.size() == 0){
            return Result.fail(323,"没有为分配班级的学生");
        }return Result.success("查询成功",studentList);
    }


    /**
     * 更新图片
     *
     * @param account
     * @return
     */
    @PostMapping("updateHeadImg")
    public Result updateHeadImg(@RequestParam("account") String account, @RequestParam("headImg") String headImg) {
        studentService.updateHeadImg(account, headImg);
        return Result.success("更新成功", null);
    }

    //    通过id教师更新学生的部分信息
    @PostMapping("updateById")
    public Result updateStuByTea(@RequestBody Student student) {
        //这里通过id修改呗,看看修改的和数据库有没有重名
        Student student1 = studentService.queryStuByAccount(student.getAccount());
        if (student1 == null) {
            int i = studentService.updateStuByTea(student);
            return Result.success("更新成功", student);
        } else {
            long id = student1.getId();
            if (id == student.getId()) {
                int i = studentService.updateStuByTea(student);
                return Result.success("更新成功", student);
            }
            return Result.fail(444, "新账号已存在，更改失败");
        }

    }

    @GetMapping("queryByAccount/{account}")
    public Result queryBaAccount(@PathVariable("account") String account) {
        System.out.println("666"+account);
        Student student = studentService.queryStuBysAccount(account);
//        System.out.println(account);

        return Result.success("okk", student);
    }

    @PostMapping("addToClass")
    public Result addToClass(@RequestBody StudentListVo studentListVo){
         return studentService.addToClass(studentListVo);
    }

    @PostMapping("rmFromClass")
    public Result rmFromClass(@RequestBody StudentListVo studentListVo){
        return studentService.rmFromClass(studentListVo);
    }

    @PostMapping("queryAllByClass")
    public Result queryAllByClass(@RequestBody StudentListVo currentClassName ){
       String className = currentClassName.getCurrentClassName();
       List<Student>  studentList=  studentService.queryAllByClass(className);
       if(studentList.size() ==0){
           return Result.fail(666,"此班没有学生");
       }
       return Result.success("查询成功",studentList);
    }

}
